package com.studyyang.workmodoul.pubsub;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.studyyang.utils.RabbitmqUtils;
import com.studyyang.utils.constant.RabbitMqConstant;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Scanner;
import java.util.concurrent.TimeoutException;

/**
 * 气象局
 * <p>
 * 模拟天气消息生产者
 *
 * @Author yangjin
 * @Description
 * @Date 2021/10/29/23:02
 **/
public class MetOffice {

    public static void main(String[] args) throws IOException, TimeoutException {
        //获取mq连接
        Connection mqConn = RabbitmqUtils.getRabbitMqConn();
        //获取信道连接
        Channel channel = mqConn.createChannel();
        //注意：这里就不用在声明队列了，消息生产者直接和交换机建立信道，不绑定队列
        //发送消息
        String message = new Scanner(System.in).next();
        //发布消息时，绑定交换机，不用绑定队列
        channel.basicPublish(RabbitMqConstant.PUB_SUB_EXCHANGE_WEATHER, "", null, message.getBytes(StandardCharsets.UTF_8));
        System.out.println("天气消息【" + message + "】发送成功!");
        //关闭连接
        channel.close();
        mqConn.close();
    }
}
